<?php
/**
 * 用户控制器
 * Created by  你老板
 * User: 你老板
 * Date: 2018/11/10
 * Time: 下午 5:10
 */
namespace app\home\controller;
use think\Controller;
use think\Request;
use \think\Db;
use app\admin\lib\Tree;
use app\admin\lib\Detail;
class Index extends Controller{
	public function _initialize() {
	}
   //首页查询品牌
	public function home_classs(){
    $db = Db::name('product_class');
    $list = $db->where('uid<>0')->limit(8)->select();
    echo json_encode($list);
    // dump($list);
	}



  //首页推荐车辆
 public function home_rec(){
	 $db = Db::name('product');
   $data =['rec'=>1];
	 	$list =$this->all_select($data,0,3);
   echo json_encode($list);
 }
//公共查询首页产
	public function all_select($data,$xiao,$da){
		$db = Db::name('product');
		$list = $db->where($data)->field('title,id,uid,supply,payments,rec,name,sort,hot,pic,dow_payments,low,package_price,repayments')->limit($xiao,$da)->select();
			$arr = [];
		foreach ($list as $key => $value) {

			$pic = json_decode($value['pic'],true);
			$arr[] = [
					'id'=>$value['id'],
					'title'=>$value['title'],
					'name'=>$value['name'],
					'payments'=>round($value['payments']/10000,2),
					'supply'=>$value['supply'],
					'pic'=>$pic[0],
			];
		}
		return $arr;
	}
 //首页最低首付
public function home_highs(){
 $data =['high'=>1];
 $list =$this->all_select($data,0,3);
  echo json_encode($list);
}

 //首页最低首付1
public function home_high1(){
 $data =['high'=>1];
 $list =$this->all_select($data,3,7);
  echo json_encode($list);
}

 //首页月供最低
public function home_low(){
 $data =['low'=>1];
 $list =$this->all_select($data,0,3);
 // dump($list);
  echo json_encode($list);
}

//首页月供最低1
public function home_low1(){
  $data =['low'=>1];
  $list =$this->all_select($data,3,7);
  echo json_encode($list);
}
//首页直降
public function home_direct(){
  $data =['direct'=>1];
  $list =$this->all_select($data,0,3);
  echo json_encode($list);
}
//首页直降1
public function home_direct1(){
  $data =['direct'=>1];
  $list =$this->all_select($data,3,7);
  echo json_encode($list);
}
//浏览历史
public function browsing_history(){
  $db = Db::name('browsing_history');

		if (isset($_POST['id'])){
				$user = $_POST['id'];
  $data = ['member_id' => $user];
  $list = $db->where($data)->select();
  $arr = '';
  foreach ($list as $key => $value) {
    $arr .= $value['product_id'].',';
  }
  $datad['id']=  array('in',$arr);
  $listd =$this->all_select($datad,0,7);
		}
  echo json_encode($listd);
}
  //首页公共查询车辆
  public function productall($arr,$kaishi=0,$jieshu=3){
    $db = Db::name('product');
    $list = $db->where($arr)->field('title,id,uid,rec,name,sort,hot,pic,dow_payments,low,package_price,repayments')->limit($kaishi.','.$jieshu)->select();
    foreach ($list as $key => $value) {
      // $title = $this->pinpai(['id'=>$value['uid']]);
      $pic = json_decode($value['pic'],true);
      // 月供=[(指导价+必须花费上牌保险等)-（首付比例*指导价）]*年份利息/还款期数
      //
      // 首付=指导价*首付比例+必要花费+商业保险
      //
      // 必要花费=购置税+上牌费+车船税+交强险
      $jine = $this->cheshiyuegong($value['id']);
      // dump();
      //统计首付
      $shoufu = round($jine['shoufus']/10000,2);
      $arrd[] = [
          'id'=>$value['id'],
          'dow_payments'=>$shoufu,
          'rec'=>$value['rec'],
          'name'=>$value['name'],
          'sort'=>$value['sort'],
          'hot'=>$value['hot'],
          'pic'=>$pic['0'],
          'yuegong'=>$jine['yuegong'],
          'title'=>$value['title'],
          'nianfen'=>$jine['nianfen'],
          'shoufu'=>$jine['shoufu'],
      ];
    }
        // dump($arrd);die;
    return $arrd;
  }

  //查询所属品牌
  public function pinpai($data){
    $product_subclass = Db::name('product_subclass');
    $product_class = Db::name('product_class');
    $classname = $product_subclass->where($data)->find();
    $list = $product_class->where(['id'=>$classname['pid']])->find();
    return $list['name'];
  }


  //测试月宫
  public function cheshiyuegong($id){
      $db = Db::name('product');
      $list = $db->where(['id'=>$id])->field('dow_payments,guidance_price,repayments,necessary_charges,commercial_insurance')->find();
      $info = Detail::yuegong($list);
      return $info;
  }
	//我的消息
  public function message(){
		$db = Db::name('messages');
		$data['user_id'] = $_POST['user_id'];
		$arr = '';
		$list = $db->where($data)->select();
		$numb = ['1','2','3'];
		foreach ($list as $key => $value) {
				if ($value['uid'] == 1) {
						$arr['ids1'][$key] = $value;
						if ($value['type'] == '0') {
								$arr['ids1']['ok'] = 1;
							}else{
								$arr['ids1']['ok'] = 0;
							}
				}
				if ($value['uid'] == 2) {
						$arr['ids2'][$key] = $value;
						if ($value['type'] == '0') {
								$arr['ids2']['ok'] = 1;
							}else{
								$arr['ids2']['ok'] = 0;
							}
				}
				if ($value['uid'] == 3) {
						$arr['ids3'][$key] = $value;
						if ($value['type'] == '0') {
								$arr['ids3']['ok'] = 1;
							}else{
								$arr['ids3']['ok'] = 0;
							}
				}
		}

		echo json_encode($arr);
	}
	//修改状态
	public function savemessage(){
			$db = Db::name('messages');
			$save['id'] = $_POST['id'];
			$db->where($save)->update(['type'=>'1']);
	}
	//判断权限
	public function role_nav(){
			$db = Db::name('home_role');
			$member = Db::name('member');
			$data['id'] = $_POST['userid'];
			$user = $member->where($data)->find();
			$con = $db->where(['id'=>$user['role_id']])->find();
			echo $con['content'];


	}
}
